clear all
set more off, perm
local mydir "`1'"
global Tables 	`mydir'/TS/Tables
global Work   	`mydir'/TS/DataWork
global Raw    	`mydir'/TS/DataRaw
global WRDS  	`mydir'/WRDS
/* ********************************************************************* */
* DEAL WITH DIVIVDEND DATA ONLY
* [#3A] load forecasts for dividend from IBES Unadjusted file
import sas using $WRDS/IBES/statsumu_xepsus.sas7bdat, clear case(lower)
keep if (index(measure,"DPS")>0 & usfirm==1 & index(curcode,"USD")>0) 

replace ticker=trim(ticker)
drop if index(fpi,"A") | index(fpi,"B") | index(fpi,"C") | index(fpi,"D")
destring fpi, replace
keep if fpi<=9
keep ticker oftic cname cusip statpers fpi fpedats medest   
recast str cname
compress

reshape wide medest fpedats, i(ticker oftic cname cusip statpers) j(fpi)
order *, sequential
order statpers 
drop fpedats0

*use quarterly data to build one-year forecast
replace medest1 =medest6+medest7+medest8+medest9 	if  medest1==. & (medest6+medest7+medest8+medest9!=medest2)
replace fpedats1=fpedats9 				if fpedats1==. & (fpedats2>fpedats9)
drop *6 *7 *8 *9 

forvalues x=2(1)5 {
	local j=`x'-1
	replace medest`x' =medest`j'*(1+medest0/100) 	if medest`j'>0 & medest`x'==.
	replace fpedats`x'=fpedats`j'+365		if fpedats`x'==.
}
generate  medest6=medest5*(1+medest0/100) if medest5>0
generate fpedats6=fpedats5+365

generate  medest7=medest6*(1+medest0/100) if medest6>0
generate fpedats7=fpedats6+365

drop medest0
keep if medest1<. | medest2<. | medest3<. | medest4<. | medest5<. | medest6<. | medest7<.
reshape long medest fpedats, i(ticker oftic cname cusip statpers) j(fpi)
format fpedats %td

* firms with missing LTG --> may not not have medest(t)
keep if medest<.
* ****************************************
* ****************************************
merge m:1 ticker statpers using $Raw/ibes01_brown_2019, keepusing(permco permno ticker statpers)
drop if _merge==2
drop _merge
* "/mnt/brownresearch/ECON_LaPorta/rlaporta/research/Aggregate/ibes01_brown_2019.do" generates /srv/ibes/ibes01_brown_2019.dta 
merge m:1 ticker statpers using $Raw/link_ibes_crsp2022, keepusing(permco permno ticker statpers) update
drop if _merge==2 | permco==.
drop _merge
* ****************************************
* ****************************************
* restrict IBES sample to members of S&P 
tempfile div
save "`div'", replace

* restrict to members of S&P 
import sas using $WRDS/CRSP/dsp500list.sas7bdat, clear case(lower)
joinby permno using "`div'"

keep if statpers>=start & statpers<=ending
drop start ending
count
* ****************************************
egen id=group(ticker statpers)
tsset id fpedats

tsfill
by id: ipolate medest fpedats, gen(fdps)
drop medest fpi

sort id fpedats
foreach var of varlist ticker oftic cname cusip statpers permco permno  {
	quietly: replace `var'=`var'[_n-1] if missing(`var')==1 & id==id[_n-1]  
}
generate fpi=(fpedats-statpers)/365
drop if fpi<0 | fpi+fdps==.

by id: egen max=max(fpi)
by id: egen min=min(fpi)

generate FPI=0 		if fpedats-statpers==92
replace  FPI=0 		if fpedats-statpers<=91 & fpi==min & min>(20/365) & FPI==.
replace  FPI=fpi 	if fpi==1 | fpi==2 | fpi==3 | fpi==4 | fpi==5 | fpi==6 
replace  FPI=1 		if fpi==max & fpi>.75 & fpi<1
  
keep if FPI!=.

* may have multiple FPIs=0
drop max
sort id FPI
by id FPI: egen max=max(fpi)
keep if fpi==max

tabstat fpi, by(FPI) stat(min max mean)

drop max min fpi id
rename FPI fpi
save $Work/cs01_div, replace
* ****************************************
